export default {
	data() {
		return {
			pages: {
				current: 1,
				size: 10,
			},
			isPullDownRefresh: false,
			loadStatus: "loading",
			params: {},
			list: this.type == "object" ? {} : [],
			mode:"month",
			type: "array",
			callback: "",
			isSort:false, //是否排序
			sortProp:"distance",//排序字段
			showPropListCallcack:[],//显示字段
		}
	},
	onPullDownRefresh() {
		this.pages.current = 1;
		this.isPullDownRefresh = true;
		this.getListFun();
	},
	//上拉加载
	onReachBottom() {
		if (this.loadStatus != 'nomore') {
			this.pages.current++;
			this.getListFun();
		}
	},
	methods: {
		initList() {
			this.pages.current = 1;
			this.list = this.type == "object" ? {} : [];
		},
		sorts_fun(arr) {
		  return function(a, b) {
		      var v1 = a[arr];
		      var v2 = b[arr];
		      return v1 - v2;
		  };
		},
		getListFun() {
			this.loadStatus = "loading";
			this.funAPI({
				...this.params,
				...this.pages,
				descs: "create_time"
			}).then(async ([err, data]) => {
				if (err) return;
				//关闭刷新
				if (this.isPullDownRefresh) {
					this.list = this.type == "object" ? {} : [];
					uni.stopPullDownRefresh();
					this.isPullDownRefresh = false;
				}
				if (data.pages == 0) { //一页也没有数据时
					this.loadStatus = "none";
				} else {
					this.loadStatus = (data.pages == this.pages.current) ? "nomore" :
						"loadmore";
				}
				let  list = data.records;
				if (this.type == "array") {
					list.forEach((item)=>{
						item = this.showPropListCallcack({},item);
					})
					let newArr = this.list.concat(list);
				    if(this.isSort){
						newArr = newArr.sort(this.sorts_fun(this.sortProp));
					}
					this.list = newArr;
				} else {
					let timeObj = {
						...this.list
					};
					if (this.$validatenull(data.records)) return;
					for(let i=0;i<data.records.length;i++){
						let item = data.records[i];
						let obj;
						let timeStr;
						if(this.mode == "day"){
							let yearMonth = item.createTime.substring(0, 10);
							timeStr = yearMonth.substring(0, 4) + "年" + yearMonth.substring(5,7) + "月"+yearMonth.substring(8)+"日";
							let shortTime = item.createTime.substring(11);
							 obj = {
								time: timeStr,
								shortTime,
							}
						}else {
							let yearMonth = item.createTime.substring(0, 7);
						    timeStr = yearMonth.substring(0, 4) + "年" + yearMonth.substring(5) + "月";
							let shortTime = item.createTime.substring(5);
							 obj = {
								time: timeStr,
								shortTime,
							}
						}
						
						obj = this.showPropListCallcack(obj,item);
						if(!this.$validatenull(this.callback)){
							let result = await this.callback(item);
						    Object.assign(obj, result);
						}
						if (this.$validatenull(timeObj[timeStr])) {
							timeObj[timeStr] = [obj];
						} else {
							timeObj[timeStr].push(obj);
						}
					}
					this.list = timeObj;
				}
			})
		},
	}

}
